home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / text / rtf / RTFReader.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  10.3 KB  |  371 lines

  1. package javax.swing.text.rtf;
  2.  
  3. import java.awt.Color;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.StreamTokenizer;
  8. import java.net.URL;
  9. import java.util.Dictionary;
  10. import java.util.Enumeration;
  11. import java.util.Hashtable;
  12. import javax.swing.text.MutableAttributeSet;
  13. import javax.swing.text.SimpleAttributeSet;
  14. import javax.swing.text.Style;
  15. import javax.swing.text.StyledDocument;
  16.  
  17. class RTFReader extends RTFParser {
  18.    StyledDocument target;
  19.    Dictionary parserState;
  20.    Destination rtfDestination;
  21.    MutableAttributeSet documentAttributes;
  22.    Dictionary fontTable;
  23.    Color[] colorTable;
  24.    Style[] characterStyles;
  25.    Style[] paragraphStyles;
  26.    Style[] sectionStyles;
  27.    int rtfversion;
  28.    boolean ignoreGroupIfUnknownKeyword;
  29.    int skippingCharacters;
  30.    private static Dictionary straightforwardAttributes = RTFAttributes.attributesByKeyword();
  31.    private MockAttributeSet mockery;
  32.    static Dictionary textKeywords = null;
  33.    static final String TabAlignmentKey = "tab_alignment";
  34.    static final String TabLeaderKey = "tab_leader";
  35.    static Dictionary characterSets;
  36.    static boolean useNeXTForAnsi;
  37.  
  38.    static {
  39.       textKeywords = new Hashtable();
  40.       textKeywords.put("\\", "\\");
  41.       textKeywords.put("{", "{");
  42.       textKeywords.put("}", "}");
  43.       textKeywords.put(" ", "¬†");
  44.       textKeywords.put("~", "¬†");
  45.       textKeywords.put("_", "‚Äë");
  46.       textKeywords.put("bullet", "‚Ä¢");
  47.       textKeywords.put("emdash", "‚Äî");
  48.       textKeywords.put("emspace", "‚ÄÉ");
  49.       textKeywords.put("endash", "‚Äì");
  50.       textKeywords.put("enspace", "‚ÄÇ");
  51.       textKeywords.put("ldblquote", "‚Äú");
  52.       textKeywords.put("lquote", "‚Äò");
  53.       textKeywords.put("ltrmark", "\u200e");
  54.       textKeywords.put("rdblquote", "‚Äù");
  55.       textKeywords.put("rquote", "‚Äô");
  56.       textKeywords.put("rtlmark", "\u200f");
  57.       textKeywords.put("tab", "\t");
  58.       textKeywords.put("zwj", "\u200d");
  59.       textKeywords.put("zwnj", "\u200c");
  60.       textKeywords.put("-", "‚Äß");
  61.       useNeXTForAnsi = false;
  62.       characterSets = new Hashtable();
  63.    }
  64.  
  65.    public RTFReader(StyledDocument var1) {
  66.       this.target = var1;
  67.       this.parserState = new Hashtable();
  68.       this.fontTable = new Hashtable();
  69.       this.rtfversion = -1;
  70.       this.mockery = new MockAttributeSet();
  71.       this.documentAttributes = new SimpleAttributeSet();
  72.    }
  73.  
  74.    // $FF: synthetic method
  75.    static Dictionary access$0() {
  76.       return straightforwardAttributes;
  77.    }
  78.  
  79.    // $FF: synthetic method
  80.    static MockAttributeSet access$1(RTFReader var0) {
  81.       return var0.mockery;
  82.    }
  83.  
  84.    public void begingroup() {
  85.       if (this.skippingCharacters > 0) {
  86.          this.skippingCharacters = 0;
  87.       }
  88.  
  89.       Object var1 = this.parserState.get("_savedState");
  90.       if (var1 != null) {
  91.          this.parserState.remove("_savedState");
  92.       }
  93.  
  94.       Dictionary var2 = (Dictionary)((Hashtable)this.parserState).clone();
  95.       if (var1 != null) {
  96.          var2.put("_savedState", var1);
  97.       }
  98.  
  99.       this.parserState.put("_savedState", var2);
  100.       if (this.rtfDestination != null) {
  101.          this.rtfDestination.begingroup();
  102.       }
  103.  
  104.    }
  105.  
  106.    public void close() throws IOException {
  107.       Enumeration var1 = this.documentAttributes.getAttributeNames();
  108.  
  109.       while(var1.hasMoreElements()) {
  110.          Object var2 = var1.nextElement();
  111.          this.target.putProperty(var2, this.documentAttributes.getAttribute((String)var2));
  112.       }
  113.  
  114.       ((RTFParser)this).warning("RTF filter done.");
  115.       super.close();
  116.    }
  117.  
  118.    Color defaultColor() {
  119.       return Color.black;
  120.    }
  121.  
  122.    public static void defineCharacterSet(String var0, char[] var1) {
  123.       if (var1.length < 256) {
  124.          throw new IllegalArgumentException("Translation table must have 256 entries.");
  125.       } else {
  126.          characterSets.put(var0, var1);
  127.       }
  128.    }
  129.  
  130.    public void endgroup() {
  131.       if (this.skippingCharacters > 0) {
  132.          this.skippingCharacters = 0;
  133.       }
  134.  
  135.       Dictionary var1 = (Dictionary)this.parserState.get("_savedState");
  136.       Destination var2 = (Destination)var1.get("dst");
  137.       if (var2 != this.rtfDestination) {
  138.          this.rtfDestination.close();
  139.          this.rtfDestination = var2;
  140.       }
  141.  
  142.       Dictionary var3 = this.parserState;
  143.       this.parserState = var1;
  144.       if (this.rtfDestination != null) {
  145.          this.rtfDestination.endgroup(var3);
  146.       }
  147.  
  148.    }
  149.  
  150.    public static Object getCharacterSet(String var0) throws IOException {
  151.       char[] var1 = (char[])characterSets.get(var0);
  152.       if (var1 == null) {
  153.          Object var2 = null;
  154.  
  155.          Class var3;
  156.          try {
  157.             var3 = Class.forName("javax.swing.text.rtf.RTFReader");
  158.          } catch (ClassNotFoundException var5) {
  159.             throw new InternalError("Help, I do not exist (" + var5 + ")");
  160.          }
  161.  
  162.          InputStream var6 = var3.getResourceAsStream("charsets/" + var0 + ".txt");
  163.          var1 = readCharset(var6);
  164.          defineCharacterSet(var0, var1);
  165.       }
  166.  
  167.       return var1;
  168.    }
  169.  
  170.    public void handleBinaryBlob(byte[] var1) {
  171.       if (this.skippingCharacters > 0) {
  172.          --this.skippingCharacters;
  173.       }
  174.    }
  175.  
  176.    public boolean handleKeyword(String var1) {
  177.       boolean var3 = this.ignoreGroupIfUnknownKeyword;
  178.       if (this.skippingCharacters > 0) {
  179.          --this.skippingCharacters;
  180.          return true;
  181.       } else {
  182.          this.ignoreGroupIfUnknownKeyword = false;
  183.          Object var2;
  184.          if ((var2 = textKeywords.get(var1)) != null) {
  185.             this.handleText((String)var2);
  186.             return true;
  187.          } else if (var1.equals("fonttbl")) {
  188.             this.setRTFDestination(new FonttblDestination(this));
  189.             return true;
  190.          } else if (var1.equals("colortbl")) {
  191.             this.setRTFDestination(new ColortblDestination(this));
  192.             return true;
  193.          } else if (var1.equals("stylesheet")) {
  194.             this.setRTFDestination(new StylesheetDestination(this));
  195.             return true;
  196.          } else if (var1.equals("info")) {
  197.             this.setRTFDestination(new InfoDestination(this));
  198.             return false;
  199.          } else if (var1.equals("mac")) {
  200.             this.setCharacterSet("mac");
  201.             return true;
  202.          } else if (var1.equals("ansi")) {
  203.             if (useNeXTForAnsi) {
  204.                this.setCharacterSet("NeXT");
  205.             } else {
  206.                this.setCharacterSet("ansi");
  207.             }
  208.  
  209.             return true;
  210.          } else if (var1.equals("next")) {
  211.             this.setCharacterSet("NeXT");
  212.             return true;
  213.          } else if (var1.equals("pc")) {
  214.             this.setCharacterSet("cpg437");
  215.             return true;
  216.          } else if (var1.equals("pca")) {
  217.             this.setCharacterSet("cpg850");
  218.             return true;
  219.          } else if (var1.equals("*")) {
  220.             this.ignoreGroupIfUnknownKeyword = true;
  221.             return true;
  222.          } else if (this.rtfDestination != null && this.rtfDestination.handleKeyword(var1)) {
  223.             return true;
  224.          } else {
  225.             if (var1.equals("aftncn") || var1.equals("aftnsep") || var1.equals("aftnsepc") || var1.equals("annotation") || var1.equals("atnauthor") || var1.equals("atnicn") || var1.equals("atnid") || var1.equals("atnref") || var1.equals("atntime") || var1.equals("atrfend") || var1.equals("atrfstart") || var1.equals("bkmkend") || var1.equals("bkmkstart") || var1.equals("datafield") || var1.equals("do") || var1.equals("dptxbxtext") || var1.equals("falt") || var1.equals("field") || var1.equals("file") || var1.equals("filetbl") || var1.equals("fname") || var1.equals("fontemb") || var1.equals("fontfile") || var1.equals("footer") || var1.equals("footerf") || var1.equals("footerl") || var1.equals("footerr") || var1.equals("footnote") || var1.equals("ftncn") || var1.equals("ftnsep") || var1.equals("ftnsepc") || var1.equals("header") || var1.equals("headerf") || var1.equals("headerl") || var1.equals("headerr") || var1.equals("keycode") || var1.equals("nextfile") || var1.equals("object") || var1.equals("pict") || var1.equals("pn") || var1.equals("pnseclvl") || var1.equals("pntxtb") || var1.equals("pntxta") || var1.equals("revtbl") || var1.equals("rxe") || var1.equals("tc") || var1.equals("template") || var1.equals("txe") || var1.equals("xe")) {
  226.                var3 = true;
  227.             }
  228.  
  229.             if (var3) {
  230.                this.setRTFDestination(new DiscardingDestination(this));
  231.             }
  232.  
  233.             return false;
  234.          }
  235.       }
  236.    }
  237.  
  238.    public boolean handleKeyword(String var1, int var2) {
  239.       boolean var3 = this.ignoreGroupIfUnknownKeyword;
  240.       if (this.skippingCharacters > 0) {
  241.          --this.skippingCharacters;
  242.          return true;
  243.       } else {
  244.          this.ignoreGroupIfUnknownKeyword = false;
  245.          if (var1.equals("uc")) {
  246.             this.parserState.put("UnicodeSkip", new Integer(var2));
  247.             return true;
  248.          } else if (var1.equals("u")) {
  249.             if (var2 < 0) {
  250.                var2 += 65536;
  251.             }
  252.  
  253.             ((RTFParser)this).handleText((char)var2);
  254.             Number var4 = (Number)this.parserState.get("UnicodeSkip");
  255.             if (var4 != null) {
  256.                this.skippingCharacters = var4.intValue();
  257.             } else {
  258.                this.skippingCharacters = 1;
  259.             }
  260.  
  261.             return true;
  262.          } else if (var1.equals("rtf")) {
  263.             this.rtfversion = var2;
  264.             this.setRTFDestination(new DocumentDestination(this));
  265.             return true;
  266.          } else {
  267.             if (var1.startsWith("NeXT") || var1.equals("private")) {
  268.                var3 = true;
  269.             }
  270.  
  271.             if (this.rtfDestination != null && this.rtfDestination.handleKeyword(var1, var2)) {
  272.                return true;
  273.             } else {
  274.                if (var3) {
  275.                   this.setRTFDestination(new DiscardingDestination(this));
  276.                }
  277.  
  278.                return false;
  279.             }
  280.          }
  281.       }
  282.    }
  283.  
  284.    public void handleText(String var1) {
  285.       if (this.skippingCharacters > 0) {
  286.          if (this.skippingCharacters >= var1.length()) {
  287.             this.skippingCharacters -= var1.length();
  288.             return;
  289.          }
  290.  
  291.          var1 = var1.substring(this.skippingCharacters);
  292.          this.skippingCharacters = 0;
  293.       }
  294.  
  295.       if (this.rtfDestination != null) {
  296.          this.rtfDestination.handleText(var1);
  297.       } else {
  298.          ((RTFParser)this).warning("Text with no destination. oops.");
  299.       }
  300.    }
  301.  
  302.    static char[] readCharset(InputStream var0) throws IOException {
  303.       char[] var1 = new char[256];
  304.       StreamTokenizer var3 = new StreamTokenizer(new InputStreamReader(var0));
  305.       var3.eolIsSignificant(false);
  306.       var3.commentChar(35);
  307.       var3.slashSlashComments(true);
  308.       var3.slashStarComments(true);
  309.  
  310.       for(int var2 = 0; var2 < 256; ++var2) {
  311.          int var4;
  312.          try {
  313.             var4 = var3.nextToken();
  314.          } catch (Exception var6) {
  315.             throw new IOException("Unable to read from character set file (" + var6 + ")");
  316.          }
  317.  
  318.          if (var4 != -2) {
  319.             throw new IOException("Unexpected token in character set file");
  320.          }
  321.  
  322.          var1[var2] = (char)((int)var3.nval);
  323.       }
  324.  
  325.       return var1;
  326.    }
  327.  
  328.    static char[] readCharset(URL var0) throws IOException {
  329.       return readCharset(var0.openStream());
  330.    }
  331.  
  332.    public void setCharacterSet(String var1) {
  333.       Object var2;
  334.       try {
  335.          var2 = getCharacterSet(var1);
  336.       } catch (Exception var5) {
  337.          ((RTFParser)this).warning("Exception loading RTF character set \"" + var1 + "\": " + var5);
  338.          var2 = null;
  339.       }
  340.  
  341.       if (var2 != null) {
  342.          super.translationTable = (char[])var2;
  343.       } else {
  344.          ((RTFParser)this).warning("Unknown RTF character set \"" + var1 + "\"");
  345.          if (!var1.equals("ansi")) {
  346.             try {
  347.                super.translationTable = (char[])getCharacterSet("ansi");
  348.             } catch (IOException var4) {
  349.                throw new InternalError("RTFReader: Unable to find character set resources (" + var4 + ")");
  350.             }
  351.          }
  352.       }
  353.  
  354.       this.setTargetAttribute("rtfCharacterSet", var1);
  355.    }
  356.  
  357.    protected void setRTFDestination(Destination var1) {
  358.       Dictionary var2 = (Dictionary)this.parserState.get("_savedState");
  359.       if (var2 != null && this.rtfDestination != var2.get("dst")) {
  360.          ((RTFParser)this).warning("Warning, RTF destination overridden, invalid RTF.");
  361.          this.rtfDestination.close();
  362.       }
  363.  
  364.       this.rtfDestination = var1;
  365.       this.parserState.put("dst", this.rtfDestination);
  366.    }
  367.  
  368.    private void setTargetAttribute(String var1, Object var2) {
  369.    }
  370. }
  371.